﻿using System;
using System.Web.UI;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_ReaderDtls : System.Web.UI.Page
    {
        Main main = new Main();
        IProjectReaderDtlsBLL ProjectReaderDtls = new ProjectReaderDtlsBLL();
        ComboBLL comboBLL = new ComboBLL();
        IProject projectBLL = new Project_BLL();
        IProjectJobAssigneeListBLL ProjectJobAssignee = new ProjectJobAssigneeListBLL();

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string BgmPro = Session["ProjectID"].ToString();
                ddlStatus.DataSource = ProjectReaderDtls.Loadjobstatus();
                ddlStatus.DataBind();
                ddlStatus.Items.Insert(0, new RadComboBoxItem("กรุณาระบุสถานะ", ""));

                ChangeMode(Main.EditMode.View);
            }
        }

        private void LoadGtgAssignCtrl()
        {
            string strBgmProjSeq = Session["ProjectID"].ToString();
            dtgAssignCtrl.DataSource = ProjectReaderDtls.Search(null, strBgmProjSeq, null, null, null, null, null);//Session["ProjectID"].ToString()
            dtgAssignCtrl.DataBind();
        }

        private void ClearControlData()
        {
            lblBgmProjJobAssignSeq.Text = "";
            txtTitle.Text = "";
            txtDes.Text = "";
            txtStart.SetValue("");
            txtEnd.SetValue("");
            ddlStatus.SelectedIndex = 0;
        }

        private void ChangeMode(Main.EditMode mode)
        {
            bool blnPermission = true;// comboBLL.IsCurrentEmpSeqIsHead(Session["ProjectJobSiteSeq"].ToString(), new SsMng().EmpSeq);

            switch (mode)
            {
                 case Main.EditMode.AddNew :
                    pnlInfo.Visible = true;
                    dtgAssignCtrl.Visible = false;
                    lblModeName.Text = "เพิ่มรายการใหม่";

                    AddNew.Visible = false;// && blnPermission;
                    btnOk.Visible = true;// false && blnPermission;
                    btnUpdate.Visible = false && blnPermission;
                    break;

                case Main.EditMode.Edit :
                    pnlInfo.Visible = true;
                    dtgAssignCtrl.Visible = false;
                    lblModeName.Text = "แก้ไขรายการ";

                    AddNew.Visible = false;
                    btnOk.Visible = false;
                    btnUpdate.Visible = true;
                    break;

                case Main.EditMode.View :
                    pnlInfo.Visible = false;
                    dtgAssignCtrl.Visible = true;

                    AddNew.Visible = true;
                    btnOk.Visible = false;
                    btnUpdate.Visible = false;
                    break;
            }
        }

        private bool CheckNullValue()
        {
            bool bOk = true;
            string strTitle = txtTitle.Text;
            string strDes = txtDes.Text;
            Nullable<DateTime> dtmStart = txtStart.GetDateValue();
            Nullable<DateTime> dtmEnd = txtEnd.GetDateValue();
            string strStatus = ddlStatus.SelectedValue;

            if (strTitle.Trim().Length == 0)
            {
                main.MessageAlert(Page, "ระบุหัวข้องาน");
                bOk = false;

            }
            else if (strDes.Trim().Length == 0)
            {
                main.MessageAlert(Page, "ระบุรายละเอียดงาน");
                bOk = false;
            }
            else if (dtmStart == null)
            {
                main.MessageAlert(Page, "ระบุวันที่เริ่มต้น");
                bOk = false;
            }
            else if(strStatus.Length == 0)
            {
                main.MessageAlert(Page, "ระบุสถานะ");
                bOk = false;
            }
            else if (strStatus == "2" && dtmEnd == null)
            {
                main.MessageAlert(Page, "ระบุวันที่สิ้นสุด");
                bOk = false;
            }
            else if (dtmEnd != null && dtmStart > dtmEnd)
            {
                main.MessageAlert(Page, "วันเริ่มต้นจะต้องไม่มากกว่าวันสิ้นสุด");
                bOk = false;
            }
            
            return bOk;
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            ClearControlData();
            ChangeMode(Main.EditMode.AddNew);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string BgmPro = Session["ProjectID"].ToString();
            string strTitle = txtTitle.Text;
            string strDes = txtDes.Text;
            Nullable<DateTime> dtmStart = txtStart.GetDateValue();
            Nullable<DateTime> dtmEnd = txtEnd.GetDateValue();
            string strEmpSeq = ddlEmployeeList.Text;
            string strStatus =  ddlStatus.SelectedValue;
            string strJobSiteID = (string)new SsMng().GetUss(SsMng.SsUserType.WorkingJobSiteSeq);

            if (!CheckNullValue())
            {
                return;
            }
            //if (comboBLL.IsCurrentEmpSeqIsHead(strJobSiteID, strEmpSeq))
                           
                ProjectReaderDtls.Insert(BgmPro, strTitle, strDes, dtmStart, dtmEnd, strStatus, new SsMng().EmpSeq);
                ClearControlData();
                LoadGtgAssignCtrl();
                ChangeMode(Main.EditMode.View);

                //return;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strBgmProjJobAssignSeq = lblBgmProjJobAssignSeq.Text;
            string BgmPro = Session["ProjectID"].ToString();
            string strTitle = txtTitle.Text;
            string strDes = txtDes.Text;
            Nullable<DateTime> dtmStart = txtStart.GetDateValue();
            Nullable<DateTime> dtmEnd = txtEnd.GetDateValue();
            string strStatus = ddlStatus.SelectedValue;
            
            if (!CheckNullValue())
            {
                return;
            }


            if (dtmStart > dtmEnd)
            {
                main.MessageAlert(Page, "วันเริ่มต้นจะต้องไม่มากกว่าวันสิ้นสุด");
            }
            else if (dtmStart == dtmEnd)
            {
                main.MessageAlert(Page, "วันที่เริ่ม้นต้องไม่เท่ากับวันที่สิ้นสุด");
            }
            else
            {
                ProjectReaderDtls.Update(strBgmProjJobAssignSeq, BgmPro, strTitle, strDes, dtmStart, dtmEnd, strStatus, new SsMng().EmpSeq);
                LoadGtgAssignCtrl();

                ClearControlData();
                ChangeMode(Main.EditMode.View);
             }
        }

        protected void dtgAssignCtrl_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            string strBgmProjSeq = Session["ProjectID"].ToString();
            dtgAssignCtrl.DataSource = ProjectReaderDtls.Search(null, strBgmProjSeq, null, null, null, null, null);//Session["ProjectID"].ToString()
        }

        protected void dtgAssignCtrl_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string strCommandName = e.CommandName;

            if (strCommandName == "GridEdit")
            {
                ClearControlData();
                ChangeMode(Main.EditMode.Edit);

                GridDataItem item = (GridDataItem)e.Item;

                lblBgmProjJobAssignSeq.Text = main.WebString(item["bgm_proj_job_assign_seq"].Text);
                txtTitle.Text = main.WebString(item["job_title"].Text);
                txtDes.Text = main.WebString(item["job_description"].Text);
                txtStart.SetValue(main.ConvertNullDateTime(main.WebString(item["job_start_date"].Text)));
                txtEnd.SetValue(main.ConvertNullDateTime(main.WebString(item["job_end_date"].Text)));
                ddlStatus.SelectedValue = main.WebString((item["job_status_seq"].Text));
            } 
            else if (strCommandName == "GridDelete")
            {
                GridDataItem item = (GridDataItem)e.Item;
                string strCountEmp2 = main.WebString(item["emp_count2"].Text);
                var x = main.AlwayInt(strCountEmp2);

                if (x != 0)
                {
                    main.MessageAlert(Page, "กรุณาลบข้อมูลผู้รับผิดชอบงานก่อน");
                }
                else
                {
                    string Sequence = main.WebString(item["bgm_proj_job_assign_seq"].Text);
                    ProjectReaderDtls.Delete(Sequence);
                    LoadGtgAssignCtrl();
                }

            }
            else if (strCommandName == "ViewDetail")
            {
                GridDataItem item = (GridDataItem)e.Item;
                string Sequence = main.WebString(item["bgm_proj_job_assign_seq"].Text);

                ClientScript.RegisterStartupScript(this.GetType(), "onclick", "openDetail('" + Sequence + "');", true);
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            ClearControlData();
            ChangeMode(Main.EditMode.View);
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            LoadGtgAssignCtrl();
        }
    }
}